package com.taxx.controller;

import com.taxx.common.Result;
import com.taxx.entity.SysMenu;
import com.taxx.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统菜单管理控制器
 */
@RestController
@RequestMapping("/menus")
@CrossOrigin
public class SysMenuController {
    
    @Autowired
    private SysMenuService menuService;
    
    /**
     * 获取菜单列表
     */
    @GetMapping("/list")
    public Result<List<SysMenu>> getMenuList() {
        try {
            List<SysMenu> menus = menuService.getMenuList();
            return Result.success("获取菜单列表成功", menus);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取菜单树
     */
    @GetMapping("/tree")
    public Result<List<SysMenu>> getMenuTree() {
        try {
            List<SysMenu> menuTree = menuService.getMenuTree();
            return Result.success("获取菜单树成功", menuTree);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 根据ID获取菜单信息
     */
    @GetMapping("/{id}")
    public Result<SysMenu> getMenuById(@PathVariable Long id) {
        try {
            SysMenu menu = menuService.getMenuById(id);
            if (menu == null) {
                return Result.notFound("菜单不存在");
            }
            return Result.success("获取菜单信息成功", menu);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 创建菜单
     */
    @PostMapping
    public Result<Void> createMenu(@Validated @RequestBody SysMenu menu) {
        try {
            boolean success = menuService.createMenu(menu);
            if (success) {
                return Result.success();
            } else {
                return Result.error("菜单创建失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新菜单
     */
    @PutMapping("/{id}")
    public Result<Void> updateMenu(@PathVariable Long id, @Validated @RequestBody SysMenu menu) {
        try {
            menu.setId(id);
            boolean success = menuService.updateMenu(menu);
            if (success) {
                return Result.success();
            } else {
                return Result.error("菜单更新失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除菜单
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteMenu(@PathVariable Long id) {
        try {
            boolean success = menuService.deleteMenu(id);
            if (success) {
                return Result.success();
            } else {
                return Result.error("菜单删除失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新菜单状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateMenuStatus(@PathVariable Long id, @RequestParam Integer status) {
        try {
            boolean success = menuService.updateMenuStatus(id, status);
            if (success) {
                return Result.success();
            } else {
                return Result.error("菜单状态更新失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 根据角色ID获取菜单列表
     */
    @GetMapping("/role/{roleId}")
    public Result<List<SysMenu>> getMenusByRoleId(@PathVariable Long roleId) {
        try {
            List<SysMenu> menus = menuService.getMenusByRoleId(roleId);
            return Result.success("获取角色菜单成功", menus);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 根据用户ID获取菜单列表
     */
    @GetMapping("/user/{userId}")
    public Result<List<SysMenu>> getMenusByUserId(@PathVariable Long userId) {
        try {
            List<SysMenu> menus = menuService.getMenusByUserId(userId);
            return Result.success("获取用户菜单成功", menus);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取当前用户的菜单列表
     */
    @GetMapping("/user")
    public Result<List<SysMenu>> getCurrentUserMenus() {
        try {
            // 从SecurityContext获取当前用户信息
            String username = org.springframework.security.core.context.SecurityContextHolder
                    .getContext().getAuthentication().getName();
            
            if (username == null || "anonymousUser".equals(username)) {
                return Result.error("用户未登录");
            }
            
            List<SysMenu> menus = menuService.getMenusByUsername(username);
            return Result.success("获取用户菜单成功", menus);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
